<?php

namespace app\superadmin\controller;

use app\common\controller\SuperadminBase;
use app\superadmin\model\Role as Model;
use app\superadmin\model\Quanxian;
use think\Controller;

use think\Db;

use think\Request;

use think\Session;



class Role extends SuperadminBase

{



    public function index()

    {

        return $this->fetch();

    }



    //获取商品列表-无限定条件-查询出所有

    public function gList(Request $request)

    {

        //获取每页显示的条数

        $limit = $request->param('limit');

        //获取当前页数

        $page = $request->param('page');


        $name = $request->param('name');


        $goodList = Model::order('create_time desc')->paginate($limit, false, ['page' => $page])->toArray();

        return json(['code' => 0, 'msg' => '', 'count' => $goodList['total'], 'data' => $goodList['data']]);

    }

    
     public function add()

    {   
        if(\request()->isPost()){


            $data     = input('post.');
            $qxarr = explode(',', $data['idstr']);
            if (count($qxarr) == 1 && $qxarr[0] == "") {
                $qxarr = [];
            }
            
            $admininfo = Model::create(['name'=>$data['name'],'remark'=>$data['remark']]);

            for ($i=0; $i < count($qxarr); $i++) { 
                Quanxian::create(['directory_id'=>$qxarr[$i],'role_id'=>$admininfo->id]);
            }

            return 1;


        }else{

            $arr = Db::name('directory')->field('id, pid,spread, name as title')->select();
            $arr1 = $this->getTree($arr);

            $arr1 = json_encode($arr1);
           
            $this->assign('mulu1', $arr1);
            return $this->fetch('role/add');

        }
        

    }


    public function getTree($items,$pid ="pid") {
        $map  = [];
        $tree = [];
        foreach ($items as &$it){ $map[$it['id']] = &$it; }  //数据的ID名生成新的引用索引树
        foreach ($items as &$at){
            $parent = &$map[$at[$pid]];
            if($parent) {
                $parent['children'][] = &$at;
            }else{
                $tree[] = &$at;
            }
        }
        return $tree;
    }



    public function addgood()

    {

        $data     = input('post.');
        $qxarr = explode(',', $data['idstr']);
        if (count($qxarr) == 1 && $qxarr[0] == "") {
            $qxarr = [];
        }
        
        $admininfo = Model::create(['name'=>$data['name'],'remark'=>$data['remark']]);

        for ($i=0; $i < count($qxarr); $i++) { 
            Quanxian::create(['directory_id'=>$qxarr[$i],'role_id'=>$admininfo->id]);
        }

        return 1;
    }





    public function edit()

    {


        if(\request()->isPost()){

            $data     = input('post.');
            $qxarr = explode(',', $data['idstr']);
            if (count($qxarr) == 1 && $qxarr[0] == "") {
                $qxarr = [];
            }
            
            Model::where('id',$data['id'])->update(['name'=>$data['name'],'remark'=>$data['remark']]);

            $qxlist = Quanxian::where('role_id',$data['id'])->select();

            for ($i=0; $i < count($qxlist); $i++) { 

                   Quanxian::destroy($qxlist[$i]['id']);
            }

            for ($i=0; $i < count($qxarr); $i++) { 
                Quanxian::create(['directory_id'=>$qxarr[$i],'role_id'=>$data['id']]);
            }
            return 1;
        

        }else{

            $id       = input('id');

            $info = Model::where('id',$id)->find();

            $arr = Db::name('directory')->field('id, pid,spread,level, name as title')->select();

            $qxids = Quanxian::where('role_id',$id)->field('directory_id')->column('directory_id');


            for ($i=0; $i < count($arr); $i++) { 
                if (in_array($arr[$i]['id'], $qxids)) {
                    
                    if ($arr[$i]['level'] == 2) {
                        $arr[$i]['checked'] = true;
                    }

                }
            }

            $arr1 = $this->getTree($arr);

            $arr1 = json_encode($arr1);
           
            $this->assign('mulu1', $arr1);

            $this->assign('info', $info);

            return $this->fetch('role/edit');

        }
        

    }



    public function edit1()

    {
       $data     = input('post.');
        $qxarr = explode(',', $data['idstr']);
        if (count($qxarr) == 1 && $qxarr[0] == "") {
            $qxarr = [];
        }
        
        Model::where('id',$data['id'])->update(['name'=>$data['name'],'remark'=>$data['remark']]);

        $qxlist = Quanxian::where('role_id',$data['id'])->select();

        for ($i=0; $i < count($qxlist); $i++) { 

               Quanxian::destroy($qxlist[$i]['id']);
        }

        for ($i=0; $i < count($qxarr); $i++) { 
            Quanxian::create(['directory_id'=>$qxarr[$i],'role_id'=>$data['id']]);
        }
        return 1;

    }




    // 删除

    public function delGood()

    {

        $id = $this->request->param('id');

        $info = Model::where('id',$id)->find();

        Model::destroy($id);

        $qxlist = Quanxian::where('role_id',$id)->select();

        for ($i=0; $i < count($qxlist); $i++) { 

               Quanxian::destroy($qxlist[$i]['id']);
        }

        return 1;

    }







}

